package com.xjtu.platform.security.modifypassword;
import com.xjtu.platform.common.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/modify_password")
public class ModifyPasswordAPI {
    @Resource
    private CommonService commonService;

    @Resource
    private ModifyPasswordService modifyPasswordService;


    @RequestMapping(value = "/modify_password",method = RequestMethod.POST)
    public Result modifyPassword(@RequestBody ModifyPasswordDto dto, @RequestHeader("Token") String Token){
        if(Token == null){
            System.out.println("查询当前用户时无令牌");
        }
        try {
            CurrentUser currentUser = TokenUtils.getUserInfo(Token, commonService);
            dto.setUser_id(currentUser.getUserId());
            Integer output = modifyPasswordService.upDatePassword(dto);
            if(output == Constants.WRONG_PASSWORD)
            {
                return Result.fail(output,"密码错误");
            }else if(output == Constants.INVALID_NEW_PASSWORD)
            {
                return Result.fail(output,"新密码不合法");
            }
            return Result.success("修改密码成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail(400,"查询当前用户失败！");
    }
}
